/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Application;

import Maps.GoogleMaps;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextField;


/**
 *
 * @author Stanley
 */
public class FindPath extends Form implements CommandListener { 
    private MainMidlet midlet;
    private TextField location1;
    private TextField location2;
    private ChoiceGroup travelMode;
    private Command cancel;
    private Command find;
    
    public FindPath(MainMidlet midlet) {
        super("Find path", null);
        
        // construc text field
        location1 = new TextField("From", null, 255, TextField.ANY);
        location2 = new TextField("To", null, 255, TextField.ANY);
        
        travelMode = new ChoiceGroup("Travel mode", ChoiceGroup.EXCLUSIVE);
        travelMode.append("Driving", null);
        travelMode.append("Walking", null);
        // append items
        append(location1);
        append(location2);
        append(travelMode);
        
        this.midlet = midlet;
        // command
        cancel = new Command("Cancel", Command.CANCEL, 1);
        find = new Command("Find", Command.OK, 1);
        setCommandListener(this);
        addCommand(cancel);
        addCommand(find);
    }
    
    public void commandAction(Command c, Displayable d) {
        if (c == cancel) {
            midlet.homeShow();
        } else {
            // find path from "From location" to "To location"
            String start = location1.getString();
            String end = location2.getString();
            String travelmode = travelMode.getSelectedIndex() == 0 ? "driving" : "walking";
            int width = midlet.getHome().getWidth();
            int height = midlet.getHome().getHeight();
            try {
                GoogleMaps maps = new GoogleMaps();
                // get direction form maps.google.com
                Image map = maps.retrieveDirection(width, height, start, end, travelmode, midlet.getHome().getZoomLevel(), MapType.DEFAULT);
                // set image map into home screen
                midlet.getHome().setMap(map);
                midlet.homeShow();
            } catch (Exception e) {
                e.printStackTrace();
            }           
        }
    }
    
}
